<template>
	<view style="background-color: #F2F6FC;height: 100vh;">
		<view style="background-color: #fff;">
			<u-skeleton rows="6" :loading="detailLoading">
				<!-- 商品图 -->
				<view>
					<u-image :fade="true" duration="450" :src="detailData.image" width="100%" height="225px"></u-image>
				</view>
				<!-- 描述 -->
				<view style="padding: 15px;">
					<text class="grid-text_goods">{{detailData.goodsName}}</text>
					<view style="display: flex; padding: 10px 0 10px 0px;">
						<text class="text_common">月售：<text style="color: crimson;">{{detailData.sale}}</text></text>
					</view>
					<view style="display: flex; justify-content: space-between; align-items: center;">
						<view style="display: flex;">
							<text class="grid-text_goods">疯抢价：</text><u-icon class="text" size="20" name="rmb"
								color="#DC143C"></u-icon><text
								style="color: #DC143C; font-size: 17px;font-weight: bold;">{{detailData.price}}</text>
						</view>
						<view>
							<u-number-box button-size="28" bgColor="#FFEBCD" v-model="cart.number"
								@change="valChange"></u-number-box>
						</view>
						<view>
							<u-button @click="handleCollect" :plain="true" iconColor="#71d5a1" text="收藏"></u-button>
						</view>
					</view>
				</view>
				<view class="navigation">
					<view class="left">
						<view class="item" @click="toStore(detailData.storeId)">
							<u-icon name="home" :size="24" color="#f29100"></u-icon>
							<view class="text u-line-1">店铺</view>
						</view>
						<view class="item car" @click="toCart">
							<u-badge class="car-num" :count="9" type="error" :offset="[-3, -6]"></u-badge>
							<u-icon name="shopping-cart" :size="24" color="#18b566"></u-icon>
							<view class="text u-line-1">购物车</view>
						</view>
					</view>
					<view class="right">
						<view @click="handleAddToCart" class="cart btn u-line-1">加入购物车</view>
					</view>
				</view>
			</u-skeleton>
		</view>

		<!-- 详情和评论 -->
		<view style="margin-top: 10px; width: 100%; height: auto;background-color: #fff;">
			<u-tabs :scrollable=false :list="list1" @click="changeOpen"></u-tabs>

			<view style="padding: 20px 10px 0px 10px;">
				<!-- 宝贝详情 -->
				<view v-if="open">
					<u-parse :content="detailData.detail" />
				</view>

				<!-- 宝贝评价 -->
				<view v-else>
					<!-- 空评论 -->
					<view v-if="isEmpty">
						<u-empty mode="message" icon="http://cdn.uviewui.com/uview/empty/car.png">
						</u-empty>
					</view>

					<view v-else class="comment" v-for="(item, index) in commentList" :key="index">
						<view class="left">
							<image :src="item.avatar" mode="aspectFill"></image>
						</view>
						<view class="right">
							<view class="top">
								<view class="name">{{ item.username }}</view>
								<view>
									<u-rate :readonly=true v-model="item.score" :allowHalf=true></u-rate>
								</view>
							</view>
							<view class="content">{{ item.comment }}</view>
							<view class="bottom">
								{{ item.createTime }}
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		onLoad
	} from "@dcloudio/uni-app";
	import {
		findGoodsDetail
	} from '@/api/goods/index.js';
	import {
		findGoodsComment
	} from '@/api/comment/index.js';
	import {
		addToCart
	} from '@/api/cart/index.js';
	import {
		doCollectGoods
	} from '@/api/collectgoods/index.js';
	import {
		ref
	} from "vue";
	import {
		getToken,
		getUserInfo
	} from "@/stores/index.js";

	const token = getToken();
	const user = getUserInfo();
	const goodsId = ref(null);
	const detailLoading = ref(true);
	onLoad((option) => {
		goodsId.value = option.goodsId;
		getGoodsDetail();
		getGoodsComment();
		detailLoading.value = false;
	})

	const detailData = ref({});

	//获取商品详情数据
	const getGoodsDetail = () => {
		findGoodsDetail(goodsId.value).then((res) => {
			if (res.code === 200) {
				detailData.value = res.data;
			}
		})
	}

	const list1 = ref([{
		name: '详情',
	}, {
		name: '评论',
	}])
	const open = ref(true);
	const changeOpen = () => {
		open.value = !open.value;
	}

	//获取商品评论
	const commentList = ref([]);
	const isEmpty = ref(true);
	const getGoodsComment = () => {
		findGoodsComment(goodsId.value).then((res) => {
			if (res.code === 200) {
				commentList.value = res.data;
				if (commentList.value.length > 0) {
					isEmpty.value = false;
				}
			}
		})
	}

	//加入购物车方法
	const cart = ref({
		userId: null,
		goodsId: null,
		number: 1
	})
	const handleAddToCart = () => {
		cart.value.goodsId = goodsId.value;
		if (token === '') {
			uni.redirectTo({
				url: "/pages/login/login"
			})
		} else {
			cart.value.userId = user.id;
			addToCart(cart.value).then((res) => {
				if (res.code === 200) {
					uni.showToast({
						title: "添加成功!"
					})
				}
			})
		}
	}

	//跳到购物车页面并刷新
	const toCart = () => {
		if (token === '') {
			uni.redirectTo({
				url: "/pages/login/login"
			})
		} else {
			uni.reLaunch({
				url: "/pages/cart/cart"
			});
		}
	}

	//跳转到店铺页面
	const toStore = (storeId) => {
		uni.navigateTo({
			url: "/pages/store/store?storeId=" + storeId
		})
	}
	
	//收藏
	const collectGoods = ref({
		userId: null,
		goodsId: null
	})
	const handleCollect = () => {
		collectGoods.value.goodsId = goodsId.value;
		if (token === '') {
			uni.redirectTo({
				url: "/pages/login/login"
			})
		}else{
			collectGoods.value.userId = user.id;
			doCollectGoods(collectGoods.value).then((res) => {
				if (res.code === 200) {
					uni.showToast({
						title: "收藏成功!"
					})
				}
			})
		}
	}
</script>

<style scoped lang="scss">
	.navigation {
		display: flex;
		border: solid 2rpx #f2f2f2;
		background-color: #ffffff;
		padding: 16rpx 0;
		justify-content: space-between;
		padding: 10px;

		.left {
			display: flex;
			font-size: 20rpx;

			.item {
				margin: 0 20rpx;

				&.car {
					text-align: center;
					position: relative;

					.car-num {
						position: absolute;
						top: -10rpx;
						right: -10rpx;
					}
				}
			}
		}

		.right {
			display: flex;
			font-size: 28rpx;
			align-items: center;

			.btn {
				line-height: 66rpx;
				padding: 0 30rpx;
				border-radius: 36rpx;
				color: #ffffff;
			}

			.cart {
				background-color: #ff9900;
				margin-right: 30rpx;
			}

			.cart:active {
				background-color: #fcbd71;
			}
		}
	}

	.grid-text_goods {
		font-size: 17px;
		color: #000000;
		font-weight: bold;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden;
		text-overflow: ellipsis;
		font-family: "华文楷体";
	}

	.text_common {
		color: #909399;
		font-size: 12px;
		margin-right: 15px;
	}

	.a_style:hover {
		color: coral;
	}

	.text {
		font-size: 13px;
		color: #909399;
		/* #ifndef APP-PLUS */
		box-sizing: border-box;
		padding-top: 8px;
		margin-bottom: 20px;
		/* #endif */
	}

	.comment {
		display: flex;
		padding: 30rpx;

		.left {
			image {
				width: 64rpx;
				height: 64rpx;
				border-radius: 50%;
				background-color: #f2f2f2;
			}
		}

		.right {
			flex: 1;
			padding-left: 20rpx;
			font-size: 30rpx;

			.top {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 10rpx;

				.name {
					color: #5677fc;
				}
			}

			.content {
				margin-bottom: 10rpx;
			}

			.bottom {
				margin-top: 20rpx;
				display: flex;
				font-size: 24rpx;
				color: #9a9a9a;
			}
		}
	}
</style>